package com.example.my_music_palyer.common.tools;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 验证特定哈希值的工具
 */
public class VerifyHash {
    
    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        
        // 数据库中的实际哈希值
        String dbHash = "$2a$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2uheWG/igi.";
        String password = "123456";
        
        System.out.println("=== 验证数据库中的哈希值 ===");
        System.out.println("密码: " + password);
        System.out.println("哈希值: " + dbHash);
        
        // 验证密码
        boolean isValid = encoder.matches(password, dbHash);
        System.out.println("验证结果: " + isValid);
        
        // 生成新的哈希值进行对比
        System.out.println("\n=== 生成新哈希值进行对比 ===");
        String newHash = encoder.encode(password);
        System.out.println("新生成的哈希: " + newHash);
        
        boolean newIsValid = encoder.matches(password, newHash);
        System.out.println("新哈希验证: " + newIsValid);
        
        // 测试不同的密码
        System.out.println("\n=== 测试不同密码 ===");
        String[] testPasswords = {"123456", "12345", "1234567", "admin", "password"};
        
        for (String testPwd : testPasswords) {
            boolean result = encoder.matches(testPwd, dbHash);
            System.out.println("密码 '" + testPwd + "' 验证结果: " + result);
        }
        
        // 检查哈希值格式
        System.out.println("\n=== 哈希值格式分析 ===");
        System.out.println("哈希值长度: " + dbHash.length());
        System.out.println("是否以$2a$开头: " + dbHash.startsWith("$2a$"));
        System.out.println("是否以$2b$开头: " + dbHash.startsWith("$2b$"));
        System.out.println("是否以$2y$开头: " + dbHash.startsWith("$2y$"));
    }
}
